package com.burst.reptile.modular.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysDictData;
import com.burst.reptile.modular.system.service.ISysDictDataService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/system/dict/data")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDictDataController extends BaseController {

	private final ISysDictDataService dictDataService;

	/**
	 * 字典数据列表
	 * @param dictData
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(SysDictData dictData) {
		Page<SysDictData> page = startPage();
		Page<SysDictData> list = dictDataService.selectPageDictDataList(page, dictData);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}

	/**
	 * 导出字典数据
	 * @param dictData
	 * @return
	 */
	@Log(title = "字典数据", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('system:dict:export')")
	@GetMapping("/export")
	public Result<Object> export(SysDictData dictData) {
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
		return util.exportExcel(list, "字典数据");
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/dictType/{dictType}")
	public Result<Object> dictType(@PathVariable String dictType) {
		return Result.success(dictDataService.selectDictDataByType(dictType));
	}

	/**
	 * 新增字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:add')")
	@Log(title = "字典数据", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody SysDictData dict) {
		return Result.success(dictDataService.insertDictData(dict));
	}

	/**
	 * 修改保存字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:edit')")
	@Log(title = "字典数据", businessType = BusinessType.UPDATE)
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody SysDictData dict) {
		return Result.success(dictDataService.updateDictData(dict));
	}

	/**
	 * 删除字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{dictCodes}")
	public Result<Object> remove(@PathVariable Long[] dictCodes) {
		return Result.success(dictDataService.deleteDictDataByIds(dictCodes));
	}

}
